﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IndyMosaic.Core.Data
{
    public class PaginatedList<T> : List<T>, IPaginated
    {
        public int PageIndex  { get; private set; }
        public int PageSize   { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }

        #region IPaginated Members

        int IPaginated.PageIndex()
        {
            return PageIndex+1;
        }

        int IPaginated.PageSize()
        {
            return PageSize;
        }

        int IPaginated.TotalCount()
        {
            return TotalCount;
        }

        int IPaginated.TotalPages()
        {
            return TotalPages;
        }

        bool IPaginated.HasPreviousPage()
        {
            return HasPreviousPage;
        }

        bool IPaginated.HasNextPage()
        {
            return HasNextPage;
        }

        #endregion
    }
}
